package com.shengqi.xinnongren.work.ui.user.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpFragment;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.ui.goods.model.MyCommentBean;
import com.shengqi.xinnongren.work.ui.user.adapter.MyCommentsAdapter;
import com.shengqi.xinnongren.work.ui.user.presenter.MyCommentFPresenter;
import com.shengqi.xinnongren.work.ui.user.view.MyCommentFView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyCommentFragment extends BaseMvpFragment<MyCommentFView, MyCommentFPresenter> implements MyCommentFView {

    //type == 1为有图片,其他随意
    public static final int TYPE_ALL = 0;
    public static final int TYPE_PIC = 1;
    private static final String EXTRA_TYPE = "type";
    int mType = TYPE_ALL;


    @BindView(R.id.rv_comments)
    RecyclerView mRecyclerView;
    @BindView(R.id.pb_loading)
    ProgressBar mProgressBar;

    @BindView(R.id.sr_refresh)
    SmartRefreshLayout mRefreshLayout;

    private int mCurrentPage = 0;
    private ArrayList<MyCommentBean> mCommentBeanList;
    private MyCommentsAdapter mAdapter;

    public MyCommentFragment() {
        // Required empty public constructor
    }

    public static MyCommentFragment newInstance(int type) {
        MyCommentFragment fragment = new MyCommentFragment();
        Bundle args = new Bundle();
        args.putInt(EXTRA_TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int setLayoutResource() {
        return R.layout.fragment_my_comment;
    }


    @Override
    protected void initView() {
        mType = getArguments().getInt(EXTRA_TYPE);
        if (mType != TYPE_ALL && mType != TYPE_PIC)
            throw new RuntimeException("老铁，类型不能瞎传");
        mRefreshLayout.setOnRefreshListener(refreshLayout -> getData(false));
        mRefreshLayout.setOnLoadMoreListener(refreshLayout -> getData(true));
        mCommentBeanList = new ArrayList<>();
        mAdapter = new MyCommentsAdapter(getContext(), mCommentBeanList);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    protected void lazyLoadData() {
        getData(false);
    }

    private void getData(boolean isLoadMore) {
        if (isLoadMore)
            mCurrentPage++;
        else
            mCurrentPage = 0;
        getPresenter().getMyCommentList(mType, mCurrentPage);
    }


    @Override
    protected void initData() {

    }

    @Override
    protected MyCommentFPresenter createPresenter() {
        return new MyCommentFPresenter();
    }

    @Override
    public void showLoadingView() {

    }

    @Override
    public void hideLoadingView() {

    }

    @Override
    public void onNetworkError() {
        resetRefreshLayout();
    }

    @Override
    public void onGetMyCommentListSucceed(List<MyCommentBean> list) {
        resetRefreshLayout();
        if (mCurrentPage == 0) {
            mCommentBeanList.clear();
        }
        mCommentBeanList.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRequestFailed(String message) {
        ToastHelper.getInstance(getContext()).showWarn(message);
        resetRefreshLayout();
    }

    private void resetRefreshLayout() {
        mProgressBar.setVisibility(View.GONE);
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
    }
}
